package com.ji.TestRedis;

import org.springframework.data.redis.connection.DefaultStringRedisConnection;
import org.springframework.data.redis.connection.RedisConnection;
import org.springframework.data.redis.connection.RedisConnectionFactory;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.serializer.Jackson2JsonRedisSerializer;
import org.springframework.data.redis.serializer.RedisSerializer;
import org.springframework.data.redis.serializer.StringRedisSerializer;

/**
 * 
 * @Title:redis存储对象扩展
 * @Description:
 * @Author:吉
 * @Since:2015年9月15日
 * @Version:1.1.0
 */
public class ObjectRedisTemplate<T> extends RedisTemplate<String, T> {

	public ObjectRedisTemplate(RedisConnectionFactory connectionFactory, Class<T> clazz) {
		RedisSerializer<T> objectSerializer = new Jackson2JsonRedisSerializer<T>(clazz);
		setValueSerializer(objectSerializer);
		setHashValueSerializer(objectSerializer);
		setConnectionFactory(connectionFactory);
		afterPropertiesSet();
	}

	protected RedisConnection preProcessConnection(RedisConnection connection, boolean existingConnection) {
		return new DefaultStringRedisConnection(connection);
	}

	/**
	 * 默认构造
	 */
	public ObjectRedisTemplate() {
		RedisSerializer<String> stringSerializer = new StringRedisSerializer();
		setKeySerializer(stringSerializer);
		setHashKeySerializer(stringSerializer);
	}
}